package com.changgou.service.goods.service.impl;

import com.changgou.common.pojo.Result;
import com.changgou.goods.pojo.Brand;
import com.changgou.service.goods.dao.BrandMapper;
import com.changgou.service.goods.service.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl implements BrandService {

	@Autowired
	BrandMapper brandMapper;

	@Override
	public List<Brand> findList() {
		List<Brand> brandList = brandMapper.selectAll();
		return brandList;
	}

	@Override
	public Brand findBrandById(Integer id) {
		Brand brand = brandMapper.selectByPrimaryKey(id);
		return brand;
	}

	@Override
	@Transactional
	public void addBrand(Brand brand) {
		brandMapper.insertSelective(brand);
	}

	@Override
	@Transactional
	public void updateBrand(Brand brand) {
		brandMapper.updateByPrimaryKeySelective(brand);
	}

	@Override
	@Transactional
	public void delById(Integer id) {
		brandMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<Brand> search(Map<String, Object> map) {
		//1、先对map判空，取出其中的品牌名称和首字母
		//定义品牌名称和品牌首字母，因为if中的name和letter应该是局部变量，否则58、59报错
		//注意：还要初始化这两个变量，否则62行报错：Variable 'name' might not have been initialized
		Object name=null;
		Object letter=null;

		if(map!=null){
			name=map.get("name");
			letter=map.get("letter");
		}

		//===============================================================//
		//第2步在下面，思考逻辑按照数字走，但是代码要按照现在的走，要不然报错：Cannot resolve symbol 'example'
		//3、创建example，按ctrl+p，查看所需要的参数，这里需要class<?>
		Example example=new Example(Brand.class);
		//4、创建Criteria
		Example.Criteria criteria = example.createCriteria();

		//5、使用Criteria进行查询
		//如果用户输入了品牌名称
		if(name!=null && !"".equals(name)){//注意：不要漏掉""前面的！，否则查询出来所有的内容
			criteria.andLike("name","%"+name+"%");
		}
		//如果用户输入了品牌首字母
		if(letter!=null && !"".equals(letter)){
			criteria.andEqualTo("letter",letter);
		}

		//2、第2步在这里）调用Mapper，查询
		List<Brand> brandList = brandMapper.selectByExample(example);

		return brandList;
	}

	@Override
	public Page<Brand> searchAllAndPage(Integer pageNum, Integer pageSize) {
		//1、分页（该行代码写在查询上，就可以了）
		PageHelper.startPage(pageNum, pageSize);

		//2、查询
		Page<Brand> brandList = (Page<Brand>)brandMapper.selectAll();///使用selectAll，返回的类型是List<Brand>
		return brandList;
	}

	@Override
	public Page<Brand> searchAndPage(Map<String, Object> map, Integer pageNum, Integer pageSize) {
		//1、分页
		PageHelper.startPage(pageNum,pageSize);

		//2、查询
		Example example=new Example(Brand.class);
		Example.Criteria criteria = example.createCriteria();

		if (map!=null){
			if (map.get("name")!=null && !"".equals(map.get("name"))){
				//根据品牌名，模糊查询
				criteria.andLike("name","%"+map.get("name")+"%");
			}
			if(map.get("letter")!=null && !"".equals(map.get("letter"))){
				//根据首字母，精确查询
				criteria.andEqualTo("letter",map.get("letter"));
			}
		}

		Page<Brand> brandList = (Page<Brand>) brandMapper.selectByExample(example);
		return brandList;
	}


}
